package cn.edu.hunau.dao.impl;


import cn.edu.hunau.dao.AddressDao;
import cn.edu.hunau.exception.DaoException;
import cn.edu.hunau.pojo.Address;
import cn.edu.hunau.utils.DBUtils;

/**
 * @Author 苏佳
 * @Date 2022 11 15 22 16
 **/
public class AddressDaoImpl implements AddressDao {
    @Override
    public int insert(Address address) throws DaoException {
        String sql="insert into t_address(uid,address,receiver,tel) values(?,?,?,?)";
        int id = DBUtils.updateReturnAutoincrementPk(sql, new String[]{"id"}, address.getUser().getId(), address.getAddress(), address.getReceiver(), address.getTel());
        address.setId(id);
        return id;
    }

    @Override
    public Address queryByAid(int aid) throws DaoException {
        String sql="select address,receiver,tel from t_address where id=?";
        return DBUtils.query(sql,rs -> {
            Address a = new Address();
            while(rs.next()){
                a.setAddress(rs.getString("address"));
                a.setReceiver(rs.getString("receiver"));
                a.setTel(rs.getString("tel"));
            }
            return a;
        },aid);
    }


}
